package org.study.mybatisplus.dynamic.boot.service.impl;

import cn.hutool.core.lang.tree.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.study.mybatisplus.dynamic.boot.config.TypeEnum.DeviceTypeEnum;
import org.study.mybatisplus.dynamic.boot.mapper.DeviceRelationMapper;
import org.study.mybatisplus.dynamic.boot.model.DeviceRelation;
import org.study.mybatisplus.dynamic.boot.service.DeviceRelationService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年10月24日
 */
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Service
public class DeviceRelationServiceImpl extends ServiceImpl<DeviceRelationMapper, DeviceRelation> implements DeviceRelationService {
    private final DeviceRelationMapper deviceRelationMapper;

    @Override
    public List<Tree<String>> deviceRelationTreeList(long projectId, long powerStationId) {
        List<DeviceRelation> deviceRelationList = deviceRelationMapper.selectList(Wrappers.lambdaQuery(DeviceRelation.class)
                .eq(DeviceRelation::getProjectId, projectId)
                .eq(DeviceRelation::getPowerStationId, powerStationId));
        return this.buildDeviceRelationTree(deviceRelationList);
    }

    @Override
    public List<Tree<String>> deviceRelationTreeList4Mppt(long projectId, long powerStationId) {
        List<DeviceRelation> deviceRelationList = deviceRelationMapper.selectList(Wrappers.lambdaQuery(DeviceRelation.class)
                .eq(DeviceRelation::getProjectId, projectId)
                .eq(DeviceRelation::getPowerStationId, powerStationId)
                .isNull(DeviceRelation::getPropertyId)
                .in(DeviceRelation::getDeviceType, Arrays.asList(DeviceTypeEnum.SHU_CAI.getType(), DeviceTypeEnum.NI_BIAN_QI.getType(), DeviceTypeEnum.NI_BIAN_QI_MPPT.getType())));
        return this.buildDeviceRelationTree(deviceRelationList);
    }

    private List<Tree<String>> buildDeviceRelationTree(List<DeviceRelation> deviceRelationList) {
        List<TreeNode<String>> treeNodeList = deviceRelationList.stream().map(deviceRelation -> {
            Map<String, Object> map = Maps.newHashMap();
            map.put("projectId", deviceRelation.getProjectId());
            map.put("powerStationId", deviceRelation.getPowerStationId());
            map.put("isRealDevice", deviceRelation.getIsRealDevice());
            map.put("deviceId", deviceRelation.getDeviceId());
            map.put("deviceType", deviceRelation.getDeviceType());
            map.put("propertyId", deviceRelation.getPropertyId());
            map.put("propertyNo", deviceRelation.getPropertyNo());
            map.put("propertyType", deviceRelation.getPropertyType());
            map.put("propertyUnit", deviceRelation.getPropertyUnit());
            map.put("propertyIcon", deviceRelation.getPropertyIcon());
            map.put("sortNo", deviceRelation.getSortNo());
            return new TreeNode<String>()
                    .setId(String.valueOf(deviceRelation.getId()))
                    .setName(deviceRelation.getName())
                    .setParentId(String.valueOf(deviceRelation.getParentId()))
                    // .setWeight(deviceRelation.getSortNo())
                    .setExtra(map);
        }).collect(Collectors.toList());
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setParentIdKey("parentId");
        treeNodeConfig.setNameKey("name");
        treeNodeConfig.setWeightKey("sortNo");
        return TreeUtil.build(treeNodeList, "0", treeNodeConfig, (treeNode, tree) -> {
            tree.setId(treeNode.getId());
            tree.setParentId(treeNode.getParentId());
            // tree.setWeight(treeNode.getWeight());
            tree.setName(treeNode.getName());
            tree.putExtra("projectId", treeNode.getExtra().getOrDefault("projectId", ""));
            tree.putExtra("powerStationId", treeNode.getExtra().getOrDefault("powerStationId", ""));
            tree.putExtra("isRealDevice", treeNode.getExtra().getOrDefault("isRealDevice", ""));
            tree.putExtra("deviceId", treeNode.getExtra().getOrDefault("deviceId", ""));
            tree.putExtra("deviceType", treeNode.getExtra().getOrDefault("deviceType", ""));
            tree.putExtra("propertyId", treeNode.getExtra().getOrDefault("propertyId", ""));
            tree.putExtra("propertyNo", treeNode.getExtra().getOrDefault("propertyNo", ""));
            tree.putExtra("propertyType", treeNode.getExtra().getOrDefault("propertyType", ""));
            tree.putExtra("propertyUnit", treeNode.getExtra().getOrDefault("propertyUnit", ""));
            tree.putExtra("propertyIcon", treeNode.getExtra().getOrDefault("propertyIcon", ""));
            tree.putExtra("sortNo", treeNode.getExtra().getOrDefault("sortNo", ""));
        });
    }
}
